<?php
/**
 * 功能描述：	系统用户管理
 * @datetime	2022-08-27
 * @version   	v2.3
 * @author   	ZHL
 * @copyrigh  	2022 ZHL 版权所有
 */

class Admin_UserController extends Controller {

    public $EssayController;
    // 是否登录
    function __construct($controller, $action) {
        parent::__construct($controller, $action);
        $loginSer = new Admin_LoginServe();
        $loginSer->judgeSessionLogin($controller, $action);
        $this->EssayController = new Admin_EssayController($controller, $action);
    }

    /**
     * Notes:文章列表
     * User: ZHL
     * Date: 2022/08/18
     */
    function index(){
        $mouldCode = Fun::input('m','sysuser');
        $this->EssayController->lists($mouldCode, 'admin_user/index', 'a.id asc');
    }

    /**
     * Notes:导出
     * User: ZhuHaili
     */
    public function execl(){
        $mouldCode = Fun::input('m');
        $menu_id = Fun::input('c', 0, 'd');
        $pageNo = Fun::input('p', 0, 'd');
        $fid = Fun::input('f', 0, 'd');
        $idStr = Fun::input('i');
        $search = Fun::input('search');
        $orderby = Fun::input('o'); //排序例如：a.id_desc,a.viewseq_desc
        if($orderby){$orderby = strtr($orderby, '_', " ");} // 替换字符串
        $commModel = new Admin_CommonModel();
        $classInfo = $commModel->getEssayInfo('sysmenu', array('id'=>$menu_id));
        if(empty($classInfo)){
            htmlAlert('网络异常稍后再试');
        }
        // 获取数据
        $userSer = new Admin_UserServe();
        $result = $userSer->getEssayLists($mouldCode, $menu_id, $pageNo, $fid, $idStr, $search, $orderby);
        // 导出execl
        //$fileNameStr = '_page'.$result['page'];
        $fileNameStr = '';
        $fileTitle = $classInfo['menuname'].$fileNameStr.'_'.date('Ymd-His'); // execl文件名
        $e = array('id','title'); // 屏蔽科学计数法字段
        $csv = new ManageCSV();
        $csv->export($result['trList'], $result['thList'], $fileTitle, $e);
    }

    /**
     * Notes:文章添加
     * User: ZhuHaili
     */
    function add(){
        $this->setEssay('add', 'admin_user/add');
    }

    /**
     * Notes:文章修改
     * User: ZhuHaili
     */
    function update(){
        $this->setEssay('update', 'admin_user/update');
    }

    /**
     * Notes:当前账号修改自己
     * User: ZhuHaili
     */
    function infoup(){
        $this->setEssay('update', 'admin_user/infoup');
    }

    /**
     * Notes:文章添加 / 修改
     * User: ZhuHaili
     * Date: 2022/8/10
     * @param $param
     * @param $tmpl
     */
    public function setEssay($setFlag, $tmpl){
        $param = Fun::input();
        $subFlag = Fun::input('subFlag','');  // 数据来源标识
        //## 来自提交数据时
        if($subFlag == 'Y'){
            $essaySer = new Admin_UserServe();
            $foreachNo = isset($param['foreachno']) && intval($param['foreachno']) ? intval($param['foreachno']) : 1; // 循环添加次数
            for($i = 0; $i < $foreachNo; $i ++){
                if(isset($param['title'])){
                    $param['title'] = $i ? ($i+1).$param['title'] : $param['title'];
                }
                $result = $essaySer->setEssay($param, $setFlag);
            }
            Fun::json($result, true);
        }
        //## 来自页面预览时
        $setData['mouldCode'] = Fun::input('m','');    // 模型编码
        $setData['menu_id'] = Fun::input('c','');      // 菜单id
        $setData['fid'] = Fun::input('f','');          // 父级ID
        $setData['essay_id'] = Fun::input('i','');     // 文章id
        $setData['info'] = array();
        $commModel = new Admin_CommonModel();
        // 修改操作时
        if($setFlag == 'update'){
            // 当前文章详情
            $setData['info'] = $commModel->getEssayInfos($setData['mouldCode'], array('a.id'=>$setData['essay_id']));
            if(empty($setData['info'])){ htmlAlert('当前内容已经不存在'); }
        }
        // 多菜单关联菜单列表
        $classInfo = $commModel->getEssayInfo('sysmenu', array('id'=>$setData['menu_id']));
        if(empty($classInfo)){ htmlAlert('当前菜单已经不存在'); }
        $classSer = new Admin_MenuServe();
        $setData['classMeet'] = $classSer->getClassList($classInfo['navtype']);
        // 文章开放字段
        $fieldSer = new Admin_FieldServe();
        $mainFields = $fieldSer->getOpenClassFieldsInfos($setData['menu_id'], '2', '1');
        $viceFields = $fieldSer->getOpenClassFieldsInfos($setData['menu_id'], '2', '2');
        $fields = array_merge($mainFields, $viceFields);
        $fieldItems = FieldItemAdd::setItems($fields, $setData['info']);
        $setData['fieldItemHtml'] = isset($fieldItems['html']) ? $fieldItems['html'] : '';
        $setData['fieldItemStyle'] = isset($fieldItems['style']) ? $fieldItems['style'] : '';
        $powerSer = new Admin_PowerServe();
        $setData['roleList'] = $powerSer->powerRoleList();
        // 映射
        foreach($setData as $k => $v){
            $this->set($k, $v);
        }
        $this->set('title', $setData['mouldCode'].'_'.$setData['menu_id'].'_'.$setData['essay_id']);
        $this->display($tmpl);
    }

    /**
     * Notes:文章删除
     * User: ZHL
     * Date: 2022/7/18
     */
    public function del(){
        $mould = Fun::input('m');
        $menu_id = Fun::input('c');
        $id = Fun::input('i');
        $essaySer = new Admin_EssayServe();
        $result = $essaySer->delEssay($mould, $menu_id, $id);
        Fun::json($result, true);
    }

    /**
     * Notes:文章批量删除
     * User: ZHL
     * Date: 2022/7/18
     */
    public function dels(){
        $mould = Fun::input('m');
        $menu_id = Fun::input('c');
        $idStr = Fun::input('i');
        $essaySer = new Admin_EssayServe();
        $result = $essaySer->delEssayMore($mould, $menu_id, $idStr);
        Fun::json($result, true);
    }

    /**
     * Notes:文章批量移动
     * User: ZHL
     * Date: 2022/7/18
     */
    public function move(){
        $mould = Fun::input('m');
        $menu_id = Fun::input('c');
        $newClassId = Fun::input('c2');
        $idStr = Fun::input('i');
        $essaySer = new Admin_EssayServe();
        $result = $essaySer->moveEssayMore($mould, $menu_id, $newClassId, $idStr);
        Fun::json($result, true);
    }

    /**
     * Notes: 文章批量排序
     * User: ZHL
     * Date: 2022/7/18
     */
    public function seq(){
        $mould = Fun::input('m');
        $param = Fun::input('param');
        $essaySer = new Admin_EssayServe();
        $result = $essaySer->seqEssay($mould, $param);
        Fun::json($result, true);
    }

    /**
     * Notes:批量修改状态
     * User: ZhuHaili
     */
    public function status(){
        $mould = Fun::input('m');
        $menu_id = Fun::input('c');
        $newStatus = Fun::input('s');
        $idStr = Fun::input('i');
        $essaySer = new Admin_EssayServe();
        $result = $essaySer->updateEssayStatusMore($mould, $menu_id, $newStatus, $idStr);
        Fun::json($result, true);
    }
}